package cn.psvmc.cxzapi.service.system;

import cn.psvmc.cxzapi.config.mybatisplus.service.impl.AbstractService;
import cn.psvmc.cxzapi.exception.ServiceException;
import cn.psvmc.cxzapi.mapper.system.TPlatformMsgMapper;
import cn.psvmc.cxzapi.mapper.system.TPlatformMsgUserMapper;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.system.TPlatformMsg;
import cn.psvmc.cxzapi.model.system.TPlatformMsgUser;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 平台消息表 服务实现类
 * @Author auto
 * @Date 2024-08-15
 */
@Service
public class TPlatformMsgService extends AbstractService<TPlatformMsgMapper, TPlatformMsg> {

    @Resource
    private TPlatformMsgMapper tPlatformMsgMapper;
    @Resource
    private TPlatformMsgUserMapper platformMsgUserMapper;
    @Resource
    private TPlatformMsgUserService platformMsgUserService;
    @Resource
    private ServiceUtil serviceUtil;

    /**
    * @Date auto 2024-08-15 新增平台消息表
    * @Param tPlatformMsg 平台消息表
    * @Return int
    */
    @Transactional(rollbackFor = Exception.class)
    public void add(TPlatformMsg tPlatformMsg) {
        Integer msgType = tPlatformMsg.getMsgType();
        List<Integer> userIds = tPlatformMsg.getUserIds();
        if (msgType.equals(2) && ToolUtils.isListEmpty(userIds)) {
            throw new ServiceException("用户不能为空");
        }
        tPlatformMsgMapper.insert(tPlatformMsg);
        if (msgType.equals(2)) {//违规通知
            Integer pmid = tPlatformMsg.getPmid();
            List<TPlatformMsgUser> users = new ArrayList<>();
            TPlatformMsgUser user;
            for (Integer userId : userIds) {
                user = new TPlatformMsgUser();
                user.setUserid(userId);
                user.setPmid(pmid);
                users.add(user);
            }
            platformMsgUserService.saveBatch(users);
        } else {//其他通知
            platformMsgUserMapper.batchSave(tPlatformMsg);
        }
    }

    /**
    * @Date auto 2024-08-15 更新平台消息表
    * @Param tPlatformMsg 平台消息表模型
    * @Return int
    */
    public int edit(TPlatformMsg tPlatformMsg) {
        checkObj(tPlatformMsg.getPmid(), tPlatformMsg.getRversion());//校验数据版本号

        // 字段为null 不更新
        return tPlatformMsgMapper.updateById(tPlatformMsg);
    }

    //校验数据版本号
    private void checkObj(Integer id, Integer rversion) {
        // 通过id查询数据处理 详情
        TPlatformMsg originTPlatformMsg = this.detail(id);
        if (originTPlatformMsg == null) {
            throw new ServiceException("未查询到对应的平台消息表信息！");
        }

        // 比较数据版本号
        if (!originTPlatformMsg.getRversion().equals(rversion)) {
            throw new ServiceException("数据已更新，请刷新后重试！");
        }
    }

    /**
    * @Date auto 2024-08-15 查询平台消息表详情
    * @Param id 唯一id
    * @Return cn.psvmc.cxzapi.model.system.TPlatformMsg
    */
    public TPlatformMsg detail(Integer id){
        return tPlatformMsgMapper.selectById(id);
    }

    /**
     * @Description 查询平台消息表列表
     * @Date 2024/8/15 21:10
     * @Author YWX
     * @Param [iPage, entity]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map>
     **/
    public IPage<Map> list(Page<TPlatformMsg> iPage, SearchEntity entity) {
        return tPlatformMsgMapper.listAll(iPage, entity);
    }

    /**
     * @Description 我的消息列表
     * @Date 2024/8/15 21:43
     * @Author YWX
     * @Param [iPage, entity]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map>
     **/
    public IPage<Map> listMy(Page<TPlatformMsg> iPage, SearchEntity entity) {
        serviceUtil.setUserId(entity);//设置用户id
        return tPlatformMsgMapper.listMy(iPage, entity);
    }

    /**
     * @Description 阅读消息
     * @Date 2024/8/15 21:57
     * @Author YWX
     * @Param [id]
     * @Return cn.psvmc.cxzapi.model.system.TPlatformMsg
     **/
    public TPlatformMsg read(Integer id) {
        TPlatformMsgUser msgUser = platformMsgUserMapper.selectById(id);
        if (msgUser == null) throw new ServiceException("消息不存在！");
        TPlatformMsgUser pmu = new TPlatformMsgUser();
        pmu.setPmuid(id);
        pmu.setReaded(1);
        pmu.setReadtime(LocalDateTime.now());
        platformMsgUserMapper.updateById(pmu);
        return tPlatformMsgMapper.selectById(msgUser.getPmid());
    }

    /**
     * @Description 删除消息
     * @Date 2024/8/21 11:29
     * @Author YWX
     * @Param [id, rversion]
     * @Return void
     **/
    public void delete(Integer id, Integer rversion) {
        checkObj(id, rversion);//校验数据版本号
        tPlatformMsgMapper.deleteById(id);
    }

    /**
     * @Description 更新消息状态
     * @Date 2025/1/19 23:14
     * @Author YWX
     * @Param [pmid, msgStatus]
     * @Return void
     **/
    public void updateMsgStatus(Integer pmid, int msgStatus) {
        TPlatformMsg msg = new TPlatformMsg();
        msg.setPmid(pmid);
        msg.setMsgStatus(msgStatus);
        tPlatformMsgMapper.updateById(msg);
    }
}
